package com.alm.handle.service.impl;

import com.alm.handle.core.domain.entity.SysAlmEventsLog;
import com.alm.handle.mapper.SysAlmEventsLogMapper;
import com.alm.handle.service.SysAlmEventsLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Consumer;

@Service
public class SysAlmEventsLogServiceImpl implements SysAlmEventsLogService {
    @Resource
    private SysAlmEventsLogMapper eventsLogMapper;
    @Override
    public Integer isnertLog(String devid, Integer type) {
        SysAlmEventsLog eventsLog=new SysAlmEventsLog();
        eventsLog.setLogTime(System.currentTimeMillis());
        eventsLog.setLogType(type);
        eventsLog.setDeviceId(devid);
        eventsLogMapper.insert(eventsLog);
        return eventsLog.getId();
    }

    @Override
    public void upexistTime(String devid, Integer type,Integer id) {
        SysAlmEventsLog eventsLog=null;
        QueryWrapper queryWrapper=new QueryWrapper();
        switch (type){
            case 1:
                //登录
                queryWrapper.eq("log_type",1);
                queryWrapper.or();
                queryWrapper.eq("log_type",2);
                queryWrapper.or();
                queryWrapper.eq("log_type",4);
                queryWrapper.orderBy(true,false,"id");
                queryWrapper.last("limit 1 OFFSET 1");
                break;
            case 2:
                //离线
                queryWrapper.eq("log_type",1);
                queryWrapper.or();
                queryWrapper.eq("log_type",4);
                queryWrapper.orderBy(true,false,"id");
                queryWrapper.last("limit 1");
                break;
            case 3:
                //异常不需要统计
                return;
            case 4:
                // 重新上电
                queryWrapper.eq("log_type",1);
                queryWrapper.or();
                queryWrapper.eq("log_type",2);
                queryWrapper.or();
                queryWrapper.eq("log_type",4);
                queryWrapper.orderBy(true,false,"id");
                queryWrapper.last("limit 1 OFFSET 1");
                break;
            case 5:
                //正常
                queryWrapper.eq("log_type",3);
                queryWrapper.orderBy(true,false,"id");
                queryWrapper.last("limit 1");
                break;
            default:
                queryWrapper=null;
                break;
        }
        eventsLog = eventsLogMapper.selectOne(queryWrapper);
        if(eventsLog!=null&&id!=null&&!id.equals(eventsLog.getId())){
            eventsLog.setExistTime(System.currentTimeMillis()-eventsLog.getLogTime());
            eventsLogMapper.updateById(eventsLog);
        }
    }

    @Override
    public Integer isnertLog(String devid, Integer type, String log) {
        SysAlmEventsLog eventsLog=new SysAlmEventsLog();
        eventsLog.setLogTime(System.currentTimeMillis());
        eventsLog.setLogType(type);
        eventsLog.setDeviceId(devid);
        eventsLog.setRemakeLog(log);
        eventsLogMapper.insert(eventsLog);
        return eventsLog.getId();
    }

    @Override
    public List<SysAlmEventsLog> queryByMac(String mac) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("device_id",mac);
        queryWrapper.orderBy(true,false,"id");
        return eventsLogMapper.selectList(queryWrapper);
    }
}
